﻿using Microsoft.AspNetCore.Mvc;

namespace duqin.Common
{
    public class ServerResponse
    {
        private static IActionResult BuildResult(int code, string msg, object? data)
        {
            if (data == null)
                return new JsonResult(new { code, msg });
            return new JsonResult(new { code, msg, data });
        }


        public static IActionResult Yes(object data, string msg = "success")
        {
            return BuildResult(0, msg, data);
        }

        public static IActionResult Yes(string msg = "success")
        {
            return BuildResult(0, msg, null);
        }


        public static IActionResult No(object? data, string msg = "failure")
        {
            return BuildResult(500, msg, data);
        }

        public static IActionResult No(string msg = "failure")
        {
            return BuildResult(500, msg, null);
        }


        private static IActionResult BuildPageResult(int code, string msg, object? page)
        {
            if (page == null)
                return new JsonResult(new { code, msg });
            return new JsonResult(new { code, msg, page });
        }

        public static IActionResult Page(object? page, string msg = "success")
        {
            return BuildPageResult(0, msg, page);
        }
    }
}
